<?php
function dbconnect(){
global $db;

$db = new Database();
 
if(!mysql_ping($db->connect())){
	if(isset($db)){$db->close();}
	$db = new Database(); 
$db->connect();
	}


}

	function getchanneldetailsu($id){
		global $db;
dbconnect();

$sql = "SELECT * FROM stream_stm where u_stm = '$id'";
return $record = $db->query_first($sql); 

		
		}	

			
function check_empty($emptycheck, $text,$check){
	$errormsg = "0";
	if(empty($emptycheck)){
		error($text);
	
	$errormsg = "1";
	
	
	}
	if($check == 1){
		$errormsg = "1";
		}
	
	return $errormsg;
	}

	
	function writefile($path){
		if($_POST['updatead']){
$file = $path.".php";
$myFile = $file;
$fh = @fopen($myFile, 'w') or die("can't open file");
@chmod($myFile,0777);
$data = get_magic_quotes_gpc()==1 ? stripslashes($_POST['pinad']) : $_POST['pinad'];
@fwrite($fh, $data);
@fclose($fh);
good("Updated");

		}

	}
function readfile_($myFile){
$fh = @fopen($myFile, 'r');
$theData = @fread($fh, filesize($myFile));
@fclose($fh);
return $theData;
	}
	
	function loopmenut($array, $class, $selected){
	echo "<ul class='$class'>";
		
	foreach($array as $a => $v){
	
	if($v[1] == $selected){$selected1 = "class='selected'";}else{$selected1 = NULL;}
	
	echo "<li $selected1><a href=\"$v[0]\">$a</a>	
			";
			}
	
		echo "</ul>";
		}
	
	
					function getusers($id){
		global $db;
dbconnect();

$sql = "SELECT * FROM users_usr where id_usr = '$id'";
return $record = $db->query_first($sql); 

}	
function incrementstream($id){
	global $db;
dbconnect();
	$sql2 = "update stream_stm SET viewers = viewers + 1, date_stm = NOW() where u_stm = '$id'";
$db->query($sql2);
		
	
	}
	
function getadminad($type){
		global $db;
dbconnect();

$sql = "SELECT * FROM admin_ads where type_ads = '$type'";
return $record = $db->query_first($sql); 

}	
	function player($width, $height, $file){
	?>
    
    
<script type='text/javascript' src='<?php echo domain?>/player/jwplayer.js'></script>

<div id='mediaplayer'></div>
<script type="text/javascript">
  jwplayer('mediaplayer').setup({
    'flashplayer': '<?php echo domain?>/player/player.swf',
    'id': '<?php echo $file?>',
    'width': '<?php echo $width?>',
    'height': '<?php echo $height?>',
	'skin': '<?php echo domain?>/player/skins/bluething.zip',
	'logo.file': '<?php echo domain?>/images/logo.png',
	'logo.link': '<?php echo domain?>',
	'abouttext': '<?php echo aboutext?>',
	'aboutlink': '<?php echo domain?>',
	'streamer': '<?php echo livestream;?>?u=<?php echo $file?>',
    'file': '<?php echo $file?>',
	'logo.position': 'top-left',
	'controlbar': 'over',
'stretching': 'uniform',
	'logo.hide': 'false',
	'wmode': 'opaque',
    'allowscriptaccess': 'always', 
	'autostart': 'true',

  'plugins': {
       'sharing-2': {
           'link': '',
           'code': '',
			
         }
    }
  });
</script>
    
    
   

		
		<?php
		};
		
		
		function ads($player, $vw, $vh, $active, $userid, $type=NULL){
			

//$adminad = getadminad('300');
$protect = protectstream($player, $type);

?>
<div style="position:relative;	float:left;	display:block;	border:0px;   height:<?php echo $vh?>px;   width:<?php echo $vw?>px" />




<div class="topad1" id="memberad" style="display:none">
<div id="close1" style="opacity:0.75;filter:alpha(opacity=85); background-color:#000; width:70px; line-height:13px; font-family:Arial, Helvetica, sans-serif;color:white; font-size:10px;padding-left:4px;float:left;z-index:111;margin-top:1px; position:absolute; top:-14px" >
<div style="float:left">Closing in &nbsp;</div>
<div id="notifier2" style="float:left"></div>
<div id="countdown2" style="float:left"></div>

</div>

<div class="innerbut" id="closemainad" style="display:none"><a href="#" onClick="hidefunc('memberad');"><img src="<?php echo domain?>/images/close_button.png"></a></div>
<script type="text/javascript">

  function display( notifier2, str ) {
    document.getElementById(notifier2).innerHTML = str;
  }
	
  function toMinuteAndSecond( x ) {
    return x%60;
  }
	
  function setTimer( remain, actions ) {
    (function countdown() {
       display("countdown2", toMinuteAndSecond(remain));		
       actions[remain] && actions[remain]();
       (remain -= 1) >= 0 && setTimeout(arguments.callee, 1000);
    })();
  }

  setTimer(35, {
        0: function () {    hidefunc('memberad');   }
  });	
  setTimeout("showfunc('memberad')", 5000);
  setTimeout("showfunc('closemainad')", 10000);

</script>


<?php 

if($protect == 'play' || $active == NULL || $active == '0'){
$random =  rand(1, 2);


switch ($random){

	case 1:
 include_once(abspath.'/content/config/admin1.php');
	break;
	case 2:
include_once(abspath.'/content/config/admin2.php');
	break;
	
	}
echo "</div>";

if($active == '1' || $active == '3' ){
incrementstream($player);
player($vw,$vh,$player);
echo "</div>";

 }else if ($active == '3'){?>
<div class="embed2">
<div class="embed5"><a href="<?php echo domain?>" target="_blank"><img src="images/toplogo.png"/></a></div>
<div class="embed4">This stream is currently offline. Please refresh once live.</div>
</div>

<?php }else{?>
	
<div class="embed2">
<div class="embed5"><a href="<?php echo domain?>" target="_blank"><img src="images/toplogo.png"/></a></div>
<div class="embed4">The video may have been rejected due to a Terms of Use or copyright violation.</div>
</div>
	
	<?php }
		
}else{echo "</div>";

?>

<div class="embed2">
<div class="embed5"><a href="<?php echo domain?>/?p=channeldetails&id=<?php echo $player?>" target="_blank"><img src="images/toplogo.png"/></a></div>
<div class="embed4">This a protected stream and cannot be viewed on your site.</div>
</div>

<?php
		}}
	
function protectstream($userid, $type=NULL){
		global $db;
dbconnect();

global $allowedurl;


if(!empty($userid)){
if(empty($type)){
	
if (isset($_SERVER['HTTP_REFERER']))
{
$arrayref = parse_url($_SERVER['HTTP_REFERER']);
$arrayref = $arrayref['host'];
}else {
		return NULL;
		}


if(!empty($arrayref)){
if(in_array($arrayref, $allowedurl)){
	return 'play';
}else{

$sql = "SELECT protect_stm FROM stream_stm where u_stm = '$userid'";
$record = $db->query_first($sql); 

if($record['protect_stm'] != ''){

if (@stristr($arrayref, $record['protect_stm']) != FALSE){	
return 'play';
		}else{
			return NULL;
			
			}

}else{
return 'play';

}}
}
}else{
	return 'play';
	}
}

}



	function redirect($url){
				echo "<script>location.href='$url'</script>"; 
				}

